package com.glela.platform.service.impl;

import com.glela.common.constant.CommonConstants;
import com.glela.platform.mapper.AdvertisementMapper;
import com.glela.platform.model.Advertisement;
import com.glela.platform.model.BaseRestrictions;
import com.glela.platform.service.AdvertisementService;
import com.glela.platform.service.BaseRestrictionsService;
import com.glela.platform.util.DateUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Random;

@Service("advertisementService")
public class AdvertisementServiceImpl implements AdvertisementService{

	@Resource
	private AdvertisementMapper advertisementMapper;
	@Autowired
	private BaseRestrictionsService baseRestrictionsService;
	/**
	 * @Description: 广告新接口（可根据类型判断是否跳转H5活动详情）
	 * @Author: zhangrenjie
	 * @Date: 2018/1/12 17:07
	 *
	 */
	@Override
	public Advertisement selectAdvertisementNew(HttpServletRequest request) {
		BaseRestrictions restrictions = this.baseRestrictionsService.getBaseRestrictionsForCache("advertisement_activity");
		String url = "";
		int type = 0;
		if (null != restrictions) {
			url = restrictions.getName();
			type = restrictions.getExperience();
		}
		String phoneType = CommonConstants.PHONE_CLIENT_ANDROID;
		if (!StringUtils.isBlank(request.getHeader("phoneType"))) {
			phoneType = request.getHeader("phoneType").toLowerCase();
		}
		String nowString = DateUtils.getNowString(DateUtils.DATE_TIME_FORMAT);
//		Advertisement advertisement=advertisementMapper.selectAdvertisement();
		List<Advertisement> advertisementList = advertisementMapper.selectAdvertisementAll(nowString);
		Advertisement ad = new Advertisement();
		if (!CollectionUtils.isEmpty(advertisementList)) {
			ad = (Advertisement)this.getRandomList(advertisementList);
			if (null != ad) {
				String aId = ad.getActivity();
				if (ad.getType() == type) {//说明需要跳转H5活动详情
					ad.setActivityId(aId);
					ad.setActivity(url+"id="+aId+"&"+phoneType+"=1"+"&t="+System.currentTimeMillis());
					if(StringUtils.isNoneBlank(request.getHeader("userId"))) {
						if(StringUtils.equals(phoneType, CommonConstants.PHONE_CLIENT_ANDROID) || StringUtils.equals(phoneType, CommonConstants.PHONE_CLIENT_IOS)) {
							String _url = ad.getActivity();
							if(StringUtils.isNotBlank(_url)) {
								ad.setActivity(_url + "&talentId=" + request.getHeader("userId"));
							}
						}
					}
					
				}
			}
		}
		return ad;
	}
	/**
	 * @Description: 不查询类型为跳转H5活动页面的广告（为了兼容旧版客户端）
	 * @Author: zhangrenjie
	 * @Date: 2018/1/12 17:19
	 *
	 */
	@Override
	public Advertisement selectAdvertisement(HttpServletRequest request) {
		BaseRestrictions restrictions = this.baseRestrictionsService.getBaseRestrictionsForCache("advertisement_activity");
		int type = 0;
		if (null != restrictions) {
			type = restrictions.getExperience();
		}
		List<Advertisement> list = advertisementMapper.selectAdvertisement(type);
		Advertisement ad = new Advertisement();
		if (!CollectionUtils.isEmpty(list)) {
			ad = (Advertisement)this.getRandomList(list);
		}
		return ad;
	}

	private  Object getRandomList(List paramList){
		Random random=new Random();
		Object object = new Object();
		int temp=0;
		temp=random.nextInt(paramList.size());//将产生的随机数作为被抽list的索引
		object = (paramList.get(temp));
		return object;
	}

	
}
